package cn.flkj.lucia2a2.config;

import cn.flkj.lucia2a2.intermediate.FabricManager;
import cn.flkj.lucia2a2.intermediate.OrgManager;
import lombok.Data;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author : 州长在手 2021/1/7 下午4:45
 */
@Component
@ConfigurationProperties(prefix = "flkj2")
@Data
public class Org2Runner  implements CommandLineRunner  {


    /*init*/
    private String cc;
    private String orgMSPID;

    private boolean openTLS;
    /*setUser1*/
    private String userleagueName;
    private String userorgName;
    private String userpeerName;
    private String username;
    private String userskPath;
    private String usercertificatePath;
    /*setUser2*/
//    private String userleagueName2;
//    private String userorgName2;
//    private String userpeerName2;
//    private String username2;
//    private String userskPath2;
//    private String usercertificatePath2;

    /*addOrderer*/
    private String orderername;
    private String ordererlocation;
    private String ordererserverCrtPath;
    private String ordererclientCertPath;
    private String ordererclientKeyPath;
    /*addPeer*/
    private String peerName;
    private String peerLocation;
    private String peerEventHubLocation;
    private String peerserverCrtPath;
    private String peerclientCertPath;
    private String peerclientKeyPath;

    private String peerName1;
    private String peerLocation1;
    private String peerEventHubLocation1;
    private String peerserverCrtPath1;
    private String peerclientCertPath1;
    private String peerclientKeyPath1;

    /*setChannel*/
    private String channelName;

    public FabricManager fm;
    public OrgManager om;

    @Override
    public void run(String... args) throws Exception {

        System.out.println(">>>>>>>>>>>>>>>服务启动执行，执行加载数据等操作<<<<<<<<<<<<<");
        this.om = new OrgManager();
        try {
            this.om.init(getCc(),
                    getOrgMSPID(),
                    getUserorgName(),
                    isOpenTLS());

            this.om.setUser(getUserleagueName(),
                    getUserorgName(),
                    getUserpeerName(),
                    getUsername(),
                    getUserskPath(),
                    getUsercertificatePath());
//
//            this.om.setUser(getUserleagueName2(),
//                    getUserorgName2(),
//                    getUserpeerName2(),
//                    getUsername2(),
//                    getUserskPath2(),
//                    getUsercertificatePath2());


            this.om.addOrderer(getOrderername(),
                    getOrdererlocation(),
                    getOrdererserverCrtPath(),
                    getOrdererclientCertPath(),
                    getOrdererclientKeyPath());

            this.om.addPeer(getPeerName(),
                    getPeerLocation(),
                    getPeerEventHubLocation(),
                    getPeerserverCrtPath(),
                    getPeerclientCertPath(),
                    getPeerclientKeyPath());

            this.om.addPeer(getPeerName1(),
                    getPeerLocation1(),
                    getPeerEventHubLocation1(),
                    getPeerserverCrtPath1(),
                    getPeerclientCertPath1(),
                    getPeerclientKeyPath1());


            this.om.setChannel(getChannelName());
            // 不需要手动add
//            this.om.addChainCode(getChaincodeName(),
//                    getChaincodePath(),
//                    getGOPATH(),
//                    getChaincodePolicy(),
//                    getChaincodeName(),
//                    getProposalWaitTime());
            this.om.add();
            fm = this.om.use(getCc(), getUsername());
            //
            om.addChainCode("fabcar010802",
                    "",
                    "",
                    "",
                    "",
                    90000);

        } catch (Exception e) {
            System.out.println("『错误信息：』" + e.getMessage());
            System.out.println("----------------FM INIT FAILED & EXIT----------------");
            System.exit(0);
        }
        try {
            System.out.println(fm.getBlockchainInfo().toJSONString());
        } catch (InvalidArgumentException | ProposalException e) {
            System.out.println("『获取区块链信息错误：』" + e.getMessage());
            System.out.println("----------------FM INIT FAILED & EXIT----------------");
        }
        System.out.println("----------------FM INIT OK----------------");
    }
}
